layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'upload', 'table', 'laydate', 'util'], function () {
    var form = layui.form,
        upload = layui.upload,
        laydate = layui.laydate,
        util = layui.util,
        admin = layui.admin;

    form.verify({
        minMoney: function (value) {
            if (value<=0) {
                return '金额必须大于0';
            }
        },
        hours: function (value) {
            if (value>24 ||value<0){
                return '时间错误';
            }
        },
        minutes: function (value) {
            if (value>59 || value<0) {
                return '时间错误';
            }
        }
    });

    /**
     * 暂存
     */
    form.on('submit(cpn_form_ok)', function (data) {

        var arr = new Array();
        $("input:checkbox[name='business_service']:checked").each(function (i) { // 遍历多选框选中的值
            arr[i] = $(this).val();
        });
        data.field.business_service = arr.toString();
        data.field.strategy_id=$('#strategy').val();
        layer.confirm("确认保存吗，切记不要重复点击", {
            btn: ["确认", "取消"]
        }, function (index, layero) {
            $.ajax({
                url: "/cpnService/update",
                data: data.field,
                type: "post",
                success: function (result) {
                    if (result.code == 1) {
                        layer.msg("编辑成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                layer.closeAll();
                                location.reload();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        }, function (index) {

        });

        return false;
    });



    // 营业时间
    form.on('submit(business_form_ok)', function (data) {
        var timeData = [];
        var business_con = $('.business_con');
        business_con.each(function () {
            var id = $(this).find('input[name="id"]').val();
            var type = $(this).find('input[name="type"]').val();
            var begin_hour = $(this).find('input[name="business_start_hours"]').val();
            var begin_minute = $(this).find('input[name="business_start_minutes"]').val();
            var end_hour = $(this).find('input[name="business_end_hours"]').val();
            var end_minute = $(this).find('input[name="business_end_minutes"]').val();
            var statusType = $(this).find('input[name="business_week"]').prop("checked");
            var status;
            if (statusType){
                status = 1;
            }else {
                status = 0;
            }
            var data = {
                "id": id,
                "type": type,
                "status": status,
                "begin_hour": begin_hour,
                "begin_minute": begin_minute,
                "end_hour": end_hour,
                "end_minute": end_minute
            };
            timeData.push(data);
        });
        layer.confirm("确认保存吗，切记不要重复点击", {
            btn: ["确认", "取消"]
        }, function (index, layero) {
            $.ajax({
                url: "/cpnService/updataWorkTime",
                data: JSON.stringify(timeData),
                type: "post",
                dataType: "json",
                contentType:"application/json",
                success: function (result) {
                    if (result.code == 1) {
                        layer.msg("保存成功", {
                            offset: '32%'
                            , icon: 1
                            , time: 0
                            , btn: '知道了'
                            , btnAlign: "c"
                            , shade: 0.4
                            , yes: function () {
                                layer.closeAll();
                            }
                        })
                    } else {
                        layer.msg(result.message, {
                            offset: '32%'
                            , icon: 2
                            , time: 3000
                        }, function () {
                            layer.closeAll();
                        })
                    }
                }
            });
        }, function (index) {

        });

        return false;
    });



    // 设置营业时间弹框
    $('.time').click(function () {
        admin.popup({
            title: '设置营业时间'
            ,shade: 0
            ,anim: -1
            ,area: ['60%', '450px']
            ,content:$("#business")
            ,end:function () {
            }
        });
    });

});

